/**
 * 
 */
package com.bonana.utils;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author vesslan
 *
 */
public class DateUtils {
	public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static Date String2Date(String dateStr, String format) {
		if (dateStr == null) {
			throw new IllegalArgumentException("argument is null.");
		}

		if (format == null) {
			format = DEFAULT_DATE_FORMAT;
		}

		Date result = null;
		try {
			DateFormat fmt = new SimpleDateFormat(format);
			result = fmt.parse(dateStr);
		} catch (Exception e) {
		}

		return result;
	}

	public static long getDate() {
		long i = System.currentTimeMillis() / 1000;

		return i;
	}

	public static Date getTimestamp() {
		Date date = new Date();
		date = new Timestamp(System.currentTimeMillis());
		return date;
	}

	/**
	 * 获取当天的最早时间
	 * 
	 * @param time_secs
	 * @return
	 */
	public static long getEarestTime(long time_secs) {
		Calendar todayStart = Calendar.getInstance();
		todayStart.setTime(new Date(time_secs));
		todayStart.set(Calendar.HOUR, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime().getTime();
	}

	/**
	 * 获取当前时间的最晚时间
	 * 
	 * @param time_secs
	 * @return
	 */
	public static long getLastestTime(long time_secs) {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.setTime(new Date(time_secs));
		todayEnd.set(Calendar.HOUR, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return todayEnd.getTime().getTime();
	}

	public static Date getWeekfirstDay() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 获取本周一的日期
		Date date = null;
		try {
			date = df.parse(df.format(cal.getTime()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}

	public static Date getWeekLastDay() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
		cal.add(Calendar.WEEK_OF_YEAR, 0);
		Date date = null;
		try {
			System.out.println(df1.format(cal.getTime()));
			date = df1.parse(df1.format(cal.getTime()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	
	public static void main(String[] args) {
		System.out.println(getWeekfirstDay());
		System.out.println(getWeekLastDay());
	}

}
